#include "hdl_epit_timer.h"

void hdl_epit1_irqhandler(void)
{ 
	if(EPIT1->SR & (1<<0)) 			/* 判断比较事件发生 */
	{
		hal_led_toggle();
		// CHKPNT;
	}
	EPIT1->SR |= 1<<0; 				/* 清除中断标志位 */
}

int8_t hdl_epit1_init(uint16_t frac,unsigned int value)
{
	if(frac > 4095)
		frac = 4095;
		
	EPIT1->CR = 0;	/* 先清零CR寄存器 */
	EPIT1->CR = (1<<24 | frac << 4 | 1<<3 | 1<<2 | 1<<1);
	EPIT1->LR = value;	/* 倒计数值 */
	EPIT1->CMPR	= 0;	/* 比较寄存器，当计数器值和此寄存器值相等的话就会产生中断 */

	GIC_EnableIRQ(EPIT1_IRQn);
	system_register_irqhandler(EPIT1_IRQn, (system_irq_handler_t)hdl_epit1_irqhandler, NULL);	
	EPIT1->CR |= 1<<0;
	DBG_MSG("epit1 init\r\n");
	return SUCCESS;
}

struct hal_epit_timer_if g_hdl_epit_timer_if =
{
	.epit_timer_init = hdl_epit1_init,
};